package org.bp.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * 空判断
 * 2018年1月19日下午4:25:18
 * @author bp
 */
public class ObjectUtils {
	private static Format FORMAT;

	public static boolean isNull(final Object o) {
		return null == o;
	}

	public static boolean isNull(final String str) {
		return null == str || "".equals(str);
	}

	public static boolean isAbsNull(final String str) {
		return null == str || "".equals(str) || "".equals(str.trim());
	}

	public static boolean isNull(final Collection<?> collection) {
		return null == collection || collection.size() == 0;
	}

	public static boolean isNull(final List<?> list) {
		return null == list || list.size() == 0;
	}

	public static boolean isNull(final Set<?> set) {
		return null == set || set.size() == 0;
	}

	public static boolean isNull(final Map<?, ?> map) {
		return null == map || map.size() == 0;
	}

	public static boolean isNull(final Long lg) {
		return null == lg || lg == 0L;
	}

	public static boolean isNull(final Integer it) {
		return null == it || it == 0;
	}

	public static boolean isNull(final File file) {
		return null == file || !file.exists();
	}

	public static boolean isNull(final Object[] strs) {
		return null == strs || strs.length == 0;
	}

	public static Number getNumber(final Number number) {
		return isNull(number) ? 0L : number;
	}

	public static String numberFormat(final Number number, final String... pattern) {
		return isNull(pattern) ? ObjectUtils.FORMAT.format(number) : ObjectUtils.FORMAT.format(pattern[0]);
	}

	public static Object clone(final Object o) {
		if (null == o) {
			return null;
		}
		ByteArrayOutputStream bos = null;
		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;
		try {
			bos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(bos);
			oos.writeObject(o);
			ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
			final Object e = ois.readObject();
			return e;
		} catch (IOException var16) {
			var16.printStackTrace();
		} catch (ClassNotFoundException var17) {
			var17.printStackTrace();
		} finally {
			try {
				if (null != bos) {
					bos.close();
				}
				if (null != oos) {
					oos.close();
				}
				if (null != ois) {
					ois.close();
				}
			} catch (IOException var18) {
				var18.printStackTrace();
			}
		}
		return null;
	}

	public static boolean isNotNull(final Object o) {
		return !isNull(o);
	}

	public static boolean isNotNull(final Collection<?> collection) {
		return !isNotNull(collection);
	}

	public static boolean isNotNull(final List<?> list) {
		return !isNull(list);
	}

	public static boolean isNotNull(final Set<?> set) {
		return !isNull(set);
	}

	public static boolean isNotNull(final Map<?, ?> map) {
		return !isNull(map);
	}

	public static boolean isNotNull(final Long lg) {
		return !isNull(lg);
	}

	public static boolean isNotNull(final Integer it) {
		return !isNull(it);
	}

	public static boolean isNotNull(final File file) {
		return !isNull(file);
	}

	public static boolean isNotNull(final Object[] strs) {
		return !isNull(strs);
	}

	public static boolean isNotNull(final String str) {
		return !isNull(str);
	}
	public static boolean isNotAbsNull(final String str) {
		return !isAbsNull(str);
	}
	static {
		ObjectUtils.FORMAT = new DecimalFormat("#.##");
	}
}
